<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {
	//用户列显示list
    public function listindex()
    {
      //系统日志记录
      sys_log('管理员列表');

      if (IS_POST) {
        //分词
         $name = I('post.username');
         $start_day = strtotime(I('post.start'));
         $stop_day = strtotime(I('post.stop'));
          $worlds = zw_pscws4($name);
          $sql ="select * from admin where create_time  between ".$start_day.' and '.$stop_day.' and ';
          foreach ($worlds as  $v) {
              $sql .="username like '%".$v['word']."%' or ";
          }
          $sql = rtrim($sql,'or ');
          $users = M()->query($sql);
          $this->assign('users',$users); 

          //系统日志记录
          $str = '查询sql语句:'.$sql;
          sys_log('管理员账号查询',$str);

      }else {
      //个数
      $count = M('admin')->count();
      $this->assign('num',$count);

        //所有用户显示出来
        $admin = M('admin');          // 实例化question对象
        //$count      = $question->where('status=1')->count();// 查询满足要求的总记录数
        $count =  $admin->count();
        $Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        //$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        //问题显示  
        //$users = $admin->where('is_del=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $sql = 'select t1.*,t2.title from admin t1 inner join auth_group t2 on t1.groupid=t2.id where is_del=1 limit '.$Page->firstRow.','.$Page->listRows;
        $users = $admin->query($sql);
        $this->assign('users',$users);        
        $this->assign('page',$show);// 赋值分页输出
      }

        $this->display();
    }

    //addindex 显示
    public function listadd()
    {
      //系统日志记录
      sys_log('管理员添加列表');

      //角色
        $sql = 'select * from auth_group';
        $g_title = M()->query($sql);
        $this->assign('g_title',$g_title);

        $this->display();   
    }
    //add 
    public function add()
    {
        //print_r($_POST);exit;
        //用户
        $data['username'] = I('post.username');
        $user_Yz = M('admin')->where(array('username'=>$data['username']))->find();
       if ($user_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"用户已存在"
                );
            echo json_encode($arr);
            exit;
       }
       //手机
       $data['phone'] = I('post.phone');
       $phone_Yz = M('admin')->where(array('phone'=>$data['phone']))->find();
       if ($phone_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"手机已存在"
                );
            echo json_encode($arr);
            exit;
       }
       //邮箱
       $data['email'] = I('post.email');
       $email_Yz = M('admin')->where(array('email'=>$data['email']))->find();
       if ($email_Yz) {
            $arr = array(
                'error' =>0,
                'info'=>"邮箱已存在"
                );
            echo json_encode($arr);
            exit;
       }

       //入库
       $data['password'] = I('post.pass');
       $data['create_time'] = time();
       $data['create_ip'] =  get_client_ip();
       $data['groupid'] = I('post.role');

       $res = M('admin')->add($data);
       if ($res) {
        //入库auth_group_access 管理员-角色-中间表
        $sql = 'insert into auth_group_access (uid,group_id) values '.'('.$res.','.$data['groupid'].')';
        $access = M()->execute($sql);
          if ($access) {

            //系统日志记录
            $str = '添加账号ID:'.$res.'把新添加账号id跟角色关联 入库 管理员-角色-中间表sql:'.$sql;
            sys_log('管理员账号添加',$str);

              $arr = array(
                      'error' =>1,
                      'info'=>"注册成功"
                      );
              echo json_encode($arr);
          }else{
            //access 失败  回滚 删掉刚刚添加的数据
            M('admin')->where(array('id'=>$res))->delete();

            //系统日志记录
            $str = '回滚 删掉刚刚添加的数据id:'.$res;
            sys_log('管理员账号跟角色关联失败',$str);

              $arr = array(
                      'error' =>0,
                      'info'=>"注册失败"
                      );
              echo json_encode($arr);
          }

       }else {
        $arr = array(
                'error' =>0,
                'info'=>"注册失败"
                );
        echo json_encode($arr);
       }

    }

    //编辑显示
    public function listedit()
    { 
      //角色
        $sql = 'select * from auth_group';
        $g_title = M()->query($sql);
        $this->assign('g_title',$g_title);

        $id = I('get.id');
        $sql = 'select * from admin where id='.$id;
        $userinfo = M()->query($sql);
        $this->assign('userinfo',current($userinfo));

        //系统日志记录
        $str = '编辑账号id='.$id;
        sys_log('管理员账号编辑显示',$str);

        $this->display();
    }
    //编辑 异步接收
    public function edit()
    {
        //print_r($_POST);exit;
      $id = I('post.userid');
      //邮箱验证
      $data = M('admin')->where(array('id'=>$id))->select();
      $data = current($data);
      
      $userinfo = M('admin')->select();
      foreach ($userinfo as $v) 
      {
        $arr['email'] = I('post.email');
        if ($v["email"]!=$data['email'] && $v["email"]==$arr['email']) 
        {
          $arr = array(
          "error" => 0,
          "info" => "邮箱已存在"
          );
          echo json_encode($arr);
          exit;
        }

        //昵称 username
        $arr['username'] = I('post.username');
        if ($v["username"]!=$data['username'] && $v['username']==$arr['username']) {
          $arr = array(
          "error" => 0,
          "info" => "昵称已存在"
          );
          echo json_encode($arr);
          exit;
        }

       //手机
       $arr['phone'] = I('post.phone');
        if ($v["phone"]!=$data['phone'] && $v['phone']==$arr['phone']) {
          $arr = array(
          "error" => 0,
          "info" => "手机已存在"
          );
          echo json_encode($arr);
          exit;
        }
      } 

       //入库
       $arr['password'] = I('post.pass');
       $arr['edit_time'] = time();
       $arr['groupid'] = I('post.role');

       $res = M('admin')->where(array('id'=>$id))->save($arr);
       if ($res) {
        //对应  根据uid 修改group_id修改auth_group_access 管理员-角色-中间表
        $sql = 'update auth_group_access set group_id='.$arr['groupid'].' where uid='.$id;
        M()->execute($sql);

        $arr = array(
                'error' =>1,
                'info'=>"修改成功"
                );
        echo json_encode($arr);

        //系统日志记录
        $arr = json_encode($arr);
        $str = '编辑账号id='.$id.'编辑字段内容:'.$arr.'根据uid 修改group_id修改auth_group_access 管理员-角色-中间表 sql语句:'.$sql;
        sys_log('管理员账号编辑',$str);

       }else {

        //系统日志记录
        $str = '编辑账号id='.$id;
        sys_log('管理员账号编辑失败',$str);

        $arr = array(
                'error' =>0,
                'info'=>"修改失败"
                );
        echo json_encode($arr);
       } 
    }

    //用户停用 交互
    public function user_stop()
    {
        //print_r($_POST);exit;
      if (IS_POST) {
        $userid = I('post.userid');
        $sql = 'update admin set is_denglu=0 where id='.$userid;
        $is_denglu = M()->execute($sql);
        if ($is_denglu) {

            //系统日志记录
            $str = '账号停用id='.$userid;
            sys_log('管理员账号停用成功',$str);

            $arr = array(
                'error' =>0,
                'info'=>"用户停用成功"
                );
            echo json_encode($arr);
        }else {

            //系统日志记录
            $str = '账号停用id='.$userid;
            sys_log('管理员账号停用失败',$str);

            $arr = array(
                'error' =>1,
                'info'=>"用户停用失败"
                );
            echo json_encode($arr);
        }

      }

    }
    //用户启用 交互
    public function user_start()
    {
        //print_r($_POST);
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update admin set is_denglu=1 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {

            //系统日志记录
            $str = '账号启用id='.$userid;
            sys_log('管理员账号启用成功',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户启用成功"
                  );
              echo json_encode($arr);
          }else {

            //系统日志记录
            $str = '账号启用id='.$userid;
            sys_log('管理员账号启用失败',$str);

              $arr = array(
                  'error' =>1,
                  'info'=>"用户启用失败"
                  );
              echo json_encode($arr);
          }
      }

    }

    //del 删除状态 1未删除 0删除
    public function is_del()
    {
      //系统日志记录
      $str = '账号删除id='.I('post.userid');
      sys_log('管理员账号删除',$str);

      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update admin set is_del=0 where id='.$userid;
          $is_denglu = M()->execute($sql);
          if ($is_denglu) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
        }       
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        $res = M('admin')->delete($ids);
          if ($res) {
            //对应删除 根据uid    auth_group_access 管理员-角色-中间表
            M('auth_group_access')->delete($ids);

            //系统日志记录
            $str = '账号批量删除id='.$ids;
            sys_log('管理员账号批量删除',$str);

              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }

      }

    }



}